package com.list;

import java.io.IOException;

import android.app.Activity;  
import android.media.MediaPlayer;  
import android.media.MediaPlayer.OnCompletionListener;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
  
public class MP extends Activity implements OnClickListener{  
    MediaPlayer mp;  
    int index;  
    private boolean isPasue = false;  
    @Override
    
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.mp);  
        mp = new MediaPlayer();  
        Bundle extras = getIntent().getExtras();  
        index = Integer.valueOf(extras.getString("index"));  
  
        //Toast.makeText(MediaPlayerActivity.this, data, 2000).show();  
        TextView info = (TextView)findViewById(R.id.textViewInfo);  
        info.setText((CharSequence) main.songs.get(index));  
  
        Button btnStartMusic = (Button)findViewById(R.id.buttonStart);  
        btnStartMusic.setOnClickListener(this);    
  
        Button prev = (Button)findViewById(R.id.buttonPrev);  
        prev.setOnClickListener(new OnClickListener() {  
  
           
            public void onClick(View v) {  
                if(index > 0){  
                    index--;  
                    mp.release();  
                    TextView info = (TextView)findViewById(R.id.textViewInfo);  
                    info.setText((CharSequence) main.songs.get(index));  
                    startMusic();  
                }  
            }  
        });  
  
        Button next = (Button)findViewById(R.id.buttonNext);  
        next.setOnClickListener(new OnClickListener() {  
  
              
            public void onClick(View v) {  
                if(index == main.songs.size()-1){  
  
                }  
                else{  
                    index++;  
                    mp.release();  
                    TextView info = (TextView)findViewById(R.id.textViewInfo);  
                    info.setText((CharSequence) main.songs.get(index));  
                    startMusic();  
                }  
  
            }  
        });  
        
    }  
  
    private void startMusic() {  
         Button btnStartMusic = (Button)findViewById(R.id.buttonStart);  
         btnStartMusic.setText("Pause");  
         mp = new MediaPlayer();  
         isPasue = false;  
  
         try {  
  
            mp.setDataSource(String.valueOf(main.dataSongs.get(index)));  
            mp.prepare();  
            mp.start();  
  
        } catch (IllegalArgumentException e) {  
            e.printStackTrace();  
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        mp.setOnCompletionListener(new OnCompletionListener() {  
  
             
            public void onCompletion(MediaPlayer mp) {  
                if(index == main.songs.size()-1){  
  
                }  
                else{  
                    index++;  
                    mp.release();  
                    TextView info = (TextView)findViewById(R.id.textViewInfo);  
                    info.setText((CharSequence) main.songs.get(index));  
                    startMusic();  
                }  
            }
            
            
        });  
    }  
  
    public void onClick(View v) {  
        if(mp.isPlaying()){  
            pauseMusic();  
  
        }  
        else if(isPasue){  
            mp.start();  
            isPasue = false;  
            Button btnStartMusic = (Button)findViewById(R.id.buttonStart);  
            btnStartMusic.setText("Pause");  
        }  
        else{  
            startMusic();  
        }  
  
    }  
  
    private void pauseMusic() {  
        Button btnStartMusic = (Button)findViewById(R.id.buttonStart);  
        btnStartMusic.setText("Play");  
        isPasue = true;  
        mp.pause();  
    }  
  
}  
